//
//  RWLoginViewController.m
//  RedWine
//
//  Created by Liufangfang on 2016/12/30.
//  Copyright © 2016年 HuiBangKe. All rights reserved.
//

#import "RWLoginViewController.h"
#import "RWLoginView.h"
#import "RWRegisterViewController.h"
#import "RWForgetPasswordViewController.h"
#import "RWHomePageRootViewController.h"
#import "AppDelegate.h"

@interface RWLoginViewController ()<RWLoginViewDelegate>

@property (nonatomic, strong) RWLoginView *loginV;

@end

@implementation RWLoginViewController

#pragma mark - View LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavigation];
    [self setLoginUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Load Datas

#pragma mark - Config UI

- (void)setNavigation{
    
    self.title = @"登录";
    /*
    UIButton *backIcon = [UIButton buttonWithType:UIButtonTypeCustom];
    backIcon.frame = CGRectMake(0, 0, 30.*FTY_WIDTH_BASE, 30.*FTY_HEIGHT_BASE);
    [backIcon setImage:[UIImage imageNamed:@"return_icon"] forState:UIControlStateNormal];
    [backIcon addTarget:self action:@selector(backIconAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backBarBtnItem = [[UIBarButtonItem alloc] initWithCustomView:backIcon];
    self.navigationItem.leftBarButtonItem = backBarBtnItem;
     */
}

- (void)setLoginUI{
    
    RWLoginView *loginV = [[RWLoginView alloc] initWithFrame:CGRectMake(0, 0, FTY_SCREEN_WIDTH, FTY_SCREEN_HEIGHT)];
    [self.view addSubview:loginV];
    loginV.delegate = self;
    loginV.backgroundColor = [UIColor whiteColor];
    _loginV = loginV;
}

#pragma mark - Config Invoke Methods

- (void)backIconAction:(UIButton *)btn{

    [self dismissViewControllerAnimated:YES completion:nil];
    //[(AppDelegate *)[UIApplication sharedApplication].delegate showWindowHome:@"loginOut"];
    //回到首页
    sharedDelegate.tabBarController.selectedIndex = 0;
    
}

#pragma mark - Notificaiton Method

#pragma mark - Notificaiton Invoke Methods

#pragma mark - <UITableViewDataSource>

#pragma mark - <UITableViewDelegate>

#pragma mark - <RWLoginViewDelegate>

- (void)loginViewAction:(UIButton *)btn{

    switch (btn.tag) {
            
        case 400:{
            
            //注册
            RWRegisterViewController *registerVC = [[RWRegisterViewController alloc] init];
            [self.navigationController pushViewController:registerVC animated:YES];
            break;
        }
        case 401:{
         
            //忘记密码
            RWForgetPasswordViewController *forgetPasswordVC = [[RWForgetPasswordViewController alloc] init];
            [self.navigationController pushViewController:forgetPasswordVC animated:YES];
            break;
        }
        case 402:{
        
            //密码登录
            [FTYHTTPManager postInfoWithSubUrl:[NSString stringWithFormat:@"/shop/ybkapi.php?act=ybkapi&op=login&user_name=%@&password=%@", _loginV.phoneTF.text, _loginV.passwordTF.text] parameters:nil block:^(id result, NSError *error) {
                
                if (!error) {
                    
                    NSInteger codeInfo = [[[result objectForKey:@"err"] objectForKey:@"errorcode"] integerValue];
                    NSString *errorInfo = [[result objectForKey:@"err"] objectForKey:@"errorinfo"];
                    NSDictionary *userInfo = result[@"info"];
                    if (0 == codeInfo) {
                        [self loginSuccess:userInfo];
                    } else {
                        [[FTYToastView alloc] addTitle:errorInfo addView:self.view];
                    }
                } else {
                    NSLog(@"======%@", error);
                }
            }];
            break;
        }
        case 403:{
            //密码登录
            [FTYHTTPManager postInfoWithSubUrl:[NSString stringWithFormat:@"/shop/ybkapi.php?act=ybkapi&op=loginbymobile&member_mobile=%@&verify=%@", _loginV.phoneTF.text, _loginV.passwordTF.text] parameters:nil block:^(id result, NSError *error) {
                
                if (!error) {
                    
                    NSInteger codeInfo = [[[result objectForKey:@"err"] objectForKey:@"errorcode"] integerValue];
                    NSString *info = [[result objectForKey:@"err"] objectForKey:@"errorinfo"];
                    NSDictionary *infoDic = nil;
                    if (0 == codeInfo) {
                        [self loginSuccess:infoDic];
                    } else {
                        [[FTYToastView alloc] addTitle:info addView:self.view];
                    }
                } else {
                    NSLog(@"======%@", error);
                }
            }];
            break;
        }
            
        default:
            break;
    }
}

#pragma mark - Private Invoke Methods

- (void)loginSuccess:(NSDictionary *)userInfo{
    
    NSLog(@"=====%@===%@", userInfo, [userInfo objectForKey:@"member_id"]);
    FTYUser *user = [FTYUser sharedUser];
    
    [user writeUserIsLogin:YES];
    [user writeUserID:[userInfo objectForKey:@"member_id"]];
    [user writeUserToken:[userInfo objectForKey:@"token"]];
    [user writeUserPhoneNumber:[userInfo objectForKey:@"member_mobile"]];
    [user writeUserName:[userInfo objectForKey:@"member_name"]];
    [user writeUserSex:[userInfo objectForKey:@"member_sex"]];
    [user writeUserAvatar:[userInfo objectForKey:@"member_avatar"]];
    [user writeUserAccountBalance:[userInfo objectForKey:@"balance"]];
    [user writeUserWineBalance:[userInfo objectForKey:@"drinksbalance"]];
    [user writeUserBenefit:[userInfo objectForKey:@"recentrebate"]];
    
//     [[NSNotificationCenter defaultCenter] postNotificationName:@"RefreshLoginSuccessNotification" object:nil];
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
